package com.zlc.common.core.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.common.service.BaseServiceImpl;
import com.zlc.common.core.process.model.entity.StoneProcess;
import com.zlc.common.core.process.mapper.StoneProcessMapper;
import com.zlc.common.core.process.model.vo.StoneProcessVo;
import com.zlc.common.core.process.service.StoneProcessService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author  zl
 * @date 2023-10-17 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoneProcessServiceImpl extends BaseServiceImpl<StoneProcessMapper, StoneProcess, StoneProcessVo> implements StoneProcessService{

    @Override
    public StoneProcessVo getByProcessCode(String processCode) {
        LambdaQueryWrapper<StoneProcess> wrapper = wrapper().eq(StoneProcess::getProcessCode, processCode);
        return queryOne(wrapper);
    }

    @Override
    public List<StoneProcessVo> getListByProcessCode(String processCode) {
        LambdaQueryWrapper<StoneProcess> wrapper = wrapper().eq(StoneProcess::getProcessCode, processCode)
                .orderByDesc(StoneProcess::getCreateTime);
        return queryVoList(wrapper);
    }

    @Override
    public StoneProcessVo getByProcessId(String processId) {
        LambdaQueryWrapper<StoneProcess> wrapper = wrapper().eq(StoneProcess::getProcessId, processId);
        return queryOne(wrapper);
    }

    @Override
    public List<StoneProcessVo> getByClassId(String classId) {
        LambdaQueryWrapper<StoneProcess> wrapper = wrapper().eq(StoneProcess::getClassId, classId);
        return queryVoList(wrapper);
    }

    @Override
    public String content(String processId) {
        StoneProcessVo processVo = getByProcessId(processId);
        if(!ObjectUtils.isEmpty(processVo) && !ObjectUtils.isEmpty(processVo.getContent())){
            return new String(processVo.getContent(), StandardCharsets.UTF_8);
        }
        return null;
    }
}
